/*  cookie 封装
 *  目标：浏览器关闭自动失效，并且设置值是可以带过期时间，时间过期则获取不到指定值
 * @Author: beautifulHao 
 * @Date: 2019-07-17 10:32:30
 * @Last Modified by: beautifulHao
 * @Last Modified time: 2019-07-17 16:16:57
 */
import CookieUtils from './cookie'

export default {
  /*
  保存键值
  date 建议采用毫秒数值
   */
  save (key, value, date) {
    let data = {[key]:value}
    if (date) {
      data['date'] = date
    }

    CookieUtils.set(key,JSON.stringify(data))
  },

  /*
  读取
   */
  get (key) {

    let dataString = CookieUtils.get(key)

    if (dataString) {
      let data = JSON.parse(dataString)

      if (data) {
        if (data.date) {
          const date = new Date(data.date)
          //设置时间比当前时间晚，cookie值有效
          return date > new Date()? data[key]:undefined
        }

        return data[key]
      }
    }
    
    return undefined
  },

  /*
  删除
   */
  remove (key) {
    CookieUtils.remove(key)
  }
}